using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Starznet.SharePoint.SPDAT.ApplicationPages.Settings
{
    public class ManageItemPropertiesPage : StarznetBaseLayoutPage
    {
        protected SPGridView GvWebProperties;
        protected HyperLink HlAddItemProperty;

        private string QS
        {
            get
            {
                string sQs = string.Empty;
                if (Request.QueryString.Get("List") != null)
                    sQs += ((string.IsNullOrEmpty(sQs)) ? "" : "&") + "List=" + Request.QueryString["List"].ToString();
                if (Request.QueryString.Get("ID") != null)
                    sQs += ((string.IsNullOrEmpty(sQs)) ? "" : "&") + "ID=" + Request.QueryString["ID"].ToString();

                return sQs;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            bool bIsRoot = false;

            
            this.GvWebProperties.DataSource = GetItemProperties();
            this.GvWebProperties.DataBind();
            HlAddItemProperty.NavigateUrl = "AddEditItemProperty.aspx?" + QS;
        }

        public void GvWebProperties_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink oLnk = e.Row.FindControl("LnkAddEdit") as HyperLink;
                string sPropName = ((DataRowView)e.Row.DataItem)["PropertyName"].ToString();
                oLnk.Text = sPropName;
                oLnk.NavigateUrl = "AddEditItemProperty.aspx?Property=" + sPropName + "&" + QS;
            }
        }

        private DataTable GetItemProperties()
        {
            DataTable oTable = new DataTable();
            oTable.Columns.Add(new DataColumn("PropertyName"));
            oTable.Columns.Add(new DataColumn("PropertyValue"));

            SPListItem oItem = SPContext.Current.ListItem;

            DataRow oRow = null;
            foreach (DictionaryEntry oKey in oItem.Properties)
            {
                oRow = oTable.NewRow();
                oRow["PropertyName"] = oKey.Key;
                oRow["PropertyValue"] = oKey.Value;

                oTable.Rows.Add(oRow);
            }
            return oTable;
        }
    }
}
